package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * @Author: 15041
     * @Date: 19:52 2021/1/16
     * @Parms: [order]
     * @ReturnType: com.itheima.health.pojo.Order
     *Description: 订单保存
    */
    @Override
    public Order add(Order order) {
        log.info("订单-保存 order:{}", order);
        OrderSetting orderSetting = new OrderSetting();
        //从前端获取日期 这个日期带时分秒 需要设置
        Date date = order.getOrderDate();
        //构建新日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //只保留年月日
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
        //保存这个查询日期
        Date time = calendar.getTime();
        //设置预约日期
        orderSetting.setOrderDate(time);
        OrderSetting selectOne = orderSettingMapper.selectOne(orderSetting);
        if (selectOne == null) {
            //无预约设置 不能预约 抛出异常
            throw new BuilderException(MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        selectOne.setNumber(selectOne.getNumber() == null ? 0 : selectOne.getNumber());
        if (selectOne.getReservations() >= (selectOne.getNumber())) {
            //可以预约 但是预约已经满了
            throw new BuilderException(MessageConst.ORDER_FULL);
        }
        //如果可以预约 要求每个用户每天只能预约一次 查询是否是否存在该用户
        Order queryOrder = new Order();
        queryOrder.setOrderDate(time);
        queryOrder.setMemberId(order.getMemberId());
        queryOrder.setSetmealId(order.getSetmealId());
        int count = orderMapper.selectCount(queryOrder);
        if (count > 0) {
            throw new BuilderException(MessageConst.HAS_ORDERED);
        }
        //如果以上异常情况都没有发生 则保存预约订单
        orderMapper.insertSelective(order);
        //更新预约设置 已预约人数加一
        selectOne.setReservations(selectOne.getReservations() + 1);
        orderSettingMapper.updateByPrimaryKeySelective(selectOne);
        return order;
    }

    /**
     * @Author: 15041
     * @Date: 19:51 2021/1/16
     * @Parms: [id]
     * @ReturnType: com.itheima.health.pojo.Order
     *Description: 根据id查询数据
    */
    @Override
    public Order findById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: 15041
     * @Date: 19:52 2021/1/16
     * @Parms: []
     * @ReturnType: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     *Description: 统计饼图
    */
    @Override
    public List<Map<String, Object>> setmealByCount() {
        List<Map<String,Object>> list = orderMapper.setmealByCount();
        log.info("套餐预约数-饼形图占比统计结果：{}",list);
        return list;
    }


}
